<?php
include '../business/dao/BaseDataAccess.php';
include '../business/dao/ProductAccess.php';
include '../business/dao/ResourceAccess.php';
include '../business/model/model.php';
ini_set('display_errors',1);
$productDao = new ProductDao();
$resourceDao = new ResourceDao();
$catalog = $_POST['catalog'];
$name = $_POST['name'];
$content = $_POST['content'];

$product = new Product();
$product->setCatalog($catalog);
$product->setContent($content);
$product->setName($name);
$product->setCount(0);
$productDao->addProduct($product);
for ($i = 0; $i < 3; $i++) {
	$fileName = $_FILES["addImage$i"]['name'];
	
	if($fileName == null) {
		continue;
	}
	
	$fileSize = $_FILES["addImage$i"]['size'];
	$fileType = $_FILES["addImage$i"]['type'];
	$isThumbnail = isset($_POST["isThumbnail$i"]) ? true : false;
	$form_data = $_FILES["addImage$i"]['tmp_name'];
	$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
	
	$resource = new Resource();
	$resource->setData($data);
	$resource->setFileName($fileName);
	$resource->setFileSize($fileSize);
	$resource->setFileType($fileType);
	$resource->setType("img");
	$resourceDao->addResource($resource);
	
	if ($isThumbnail) {
		$product->setThumbnailId($resource->getId());
		$productDao->updateProduct($product);
	} else {
		$productResource = new ProductResource();
		$productResource->setProductId($product->getId());
		$productResource->setResourceId($resource->getId());
		$productDao->addProductResource($productResource);
	}
	header("Location:product_list.php");
}

?>